package com.blue.controller.workflow;

import com.blue.common.annotation.NotNull;
import com.blue.common.entity.ResponseResult;
import com.blue.dto.workflow.*;
import com.blue.service.workflow.IWorkflowService;
import com.blue.vo.workflow.BusinessFieldVO;
import com.blue.vo.workflow.WorkFlowInitVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程控制器
 *
 * @author nanjie.chen
 * @email 15696471158@163.com
 * @date 2025-03-07
 */
@RestController
@RequestMapping("/workflow")
@Tag(name = "流程控制器")
public class WorkflowController {

    @Autowired
    private IWorkflowService workflowService;

    @PostMapping("/init")
    @Operation(summary = "初始化流程")
    public ResponseResult<?> init(@RequestBody WorkFlowInitDTO requestDTO) {
        WorkFlowInitVO workFlowInitVO = workflowService.init(requestDTO);
        return ResponseResult.okResult(workFlowInitVO);
    }

    @PostMapping("/start")
    @Operation(summary = "启动流程")
    @NotNull(value = "definitionId", message = "流程定义ID不能为空！")
    @NotNull(value = "assigneeIds", message = "处理人不能为空！")
    @NotNull(value = "nextNodeId", message = "下个流程节点ID不能为空！")
    @NotNull(value = "data", message = "业务数据不能为空！")
    public ResponseResult<?> start(@RequestBody WorkflowStartDTO requestDTO) {
        workflowService.start(requestDTO);
        return ResponseResult.okResult();
    }

    @PostMapping("/commit")
    @Operation(summary = "提交流程")
    @NotNull(value = "taskId", message = "流程任务ID不能为空！")
    @NotNull(value = "instanceId", message = "流程实例ID不能为空！")
    @NotNull(value = "approvalStatus", message = "审批状态不能为空！")
    public ResponseResult<?> commit(@RequestBody WorkflowCommitDTO requestDTO) {
        workflowService.commit(requestDTO);
        return ResponseResult.okResult();
    }

    @PostMapping("/revoke")
    @Operation(summary = "撤销流程")
    @NotNull(value = "instanceId", message = "流程实例ID不能为空！")
    public ResponseResult<?> revoke(@RequestBody WorkflowRevokeDTO requestDTO) {
        workflowService.revoke(requestDTO);
        return ResponseResult.okResult();
    }

    @PostMapping("/dataDetail")
    @Operation(summary = "业务数据详情")
    @NotNull(value = "instanceId", message = "流程实例ID不能为空！")
    public ResponseResult<?> dataDetail(@RequestBody WorkflowDataDetailDTO requestDTO) {
        List<BusinessFieldVO> businessFieldVOS = workflowService.dataDetail(requestDTO);
        return ResponseResult.okResult(businessFieldVOS);
    }
}
